﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Data;
using ADOWrapper.ObjectResolver;

namespace ADOWrapper.Test.ObjectResolver
{
    [TestClass]
    public class GenericPopulatorTest
    {
        //accessory
        //uziti DataItem attributu

        [TestMethod]
        public void Populate_DataItemAttribute()
        {
            IObjectPopulator<SimplePropertyObject> p = new GenericPopulator<SimplePropertyObject>();

            Moq.Mock<IDataRecord> mock = new Moq.Mock<IDataRecord>();
            mock.SetupGet(c => c["NamedString"]).Returns(() => "NamedString");
            mock.SetupGet(c => c["NamedString1"]).Returns(() => "NamedString1");
            mock.SetupGet(c => c["NamedString2"]).Returns(() => "NamedString2");
            mock.SetupGet(c => c["IgnoredItem"]).Returns(() => "IgnoredItem");

            var result = p.Populate(mock.Object);

            Assert.AreEqual("ignored item", result.IgnoredItem);
            Assert.AreEqual("NamedString", result.NamedString1);
            Assert.AreEqual("NamedString", result.NamedString2);

        }

        [TestMethod]
        public void Populate_Access_Modifiers()
        {

            IObjectPopulator<AccesibleTestObject> p = new GenericPopulator<AccesibleTestObject>();


            Moq.Mock<IDataRecord> mock = new Moq.Mock<IDataRecord>();
            mock.SetupGet(c => c["ReadOnlyProperty"]).Returns(() => "ReadOnlyProperty");
            mock.SetupGet(c => c["PrivateSetterProperty"]).Returns(() => "PrivateSetterProperty");
            mock.SetupGet(c => c["InternalSetter"]).Returns(() => "InternalSetter");
            mock.SetupGet(c => c["InternalProperty"]).Returns(() => "InternalProperty");
            mock.SetupGet(c => c["PrivateProperty"]).Returns(() => "PrivateProperty");
            mock.SetupGet(c => c["PublicProperty"]).Returns(() => "PublicProperty");

            var result = p.Populate(mock.Object);

            //changed property
            Assert.AreEqual("PublicProperty", result.PublicProperty);
            //unchanged property
            Assert.AreEqual("private", result.PrivateSetterProperty);
            Assert.AreEqual("Read Only Property", result.ReadOnlyProperty);
            Assert.AreEqual("private property", result.GetPrivateProperty());
            Assert.AreEqual("internal property", result.InternalProperty);
            Assert.AreEqual("internal setter", result.InternalSetter);
        }

        public class AccesibleTestObject
        {

            public AccesibleTestObject()
            {
                PrivateSetterProperty = "private";
                PrivateProperty = "private property";
                InternalProperty = "internal property";
                InternalSetter = "internal setter";
            }

            //changed property
            public string PublicProperty { get; set; }
            //unchanged property
            public string ReadOnlyProperty { get { return "Read Only Property"; } }
            public string PrivateSetterProperty { get; private set; }
            private string PrivateProperty { get; set; }
            internal string InternalProperty { get; set; }
            public string InternalSetter { get; internal set; }

            public string GetPrivateProperty()
            {
                return PrivateProperty;
            }
        }

        public class SimplePropertyObject
        {
            public SimplePropertyObject()
            {
                IgnoredItem = "ignored item";
            }

            [DataItem("NamedString")]
            public string NamedString1 { get; set; }
            [DataItem("NamedString")]
            public string NamedString2 { get; set; }
            [DataItem(Ignore = true)]
            public string IgnoredItem { get; set; }
        }





    }
}

